//
//  DetailViewController.m
//  18-SQLiteHomeWork
//
//  Created by 曹曹 on 16/8/8.
//  Copyright © 2016年 曹曹. All rights reserved.
//

#import "DetailViewController.h"
#import "Student.h"

@interface DetailViewController ()
@property (weak, nonatomic) IBOutlet UILabel *nameLabel;
@property (weak, nonatomic) IBOutlet UILabel *phoneLabel;
@property (weak, nonatomic) IBOutlet UILabel *addressLabel;
@property (weak, nonatomic) IBOutlet UIImageView *avatarView;

@end

@implementation DetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setValues];
}

- (void)setValues {
    _nameLabel.text = _student.stu_name;
    _phoneLabel.text = _student.stu_phone;
    _addressLabel.text = _student.stu_address;
    NSArray *pathArray = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *avatarsDirectory = [[pathArray firstObject] stringByAppendingPathComponent:@"avatarsDirectory"];
    NSString *imagePath = [avatarsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.jpg", _student.stu_name]];
    NSData *data = [NSData dataWithContentsOfFile:imagePath];
    UIImage *image = [UIImage imageWithData:data];
    if (image) {
        _avatarView.image = image;
    } else {
        NSString *path = [avatarsDirectory stringByAppendingPathComponent:@"default.png"];
        NSData *data = [NSData dataWithContentsOfFile:path];
        UIImage *image = [UIImage imageWithData:data];
        _avatarView.image = image;
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
